import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_play_android/utils/toast_util.dart';
import 'package:flutter_play_android/widgets/custom_paper.dart';

import 'custom_view_screen.dart';
import 'home/home_screen.dart';
import 'image_picker_screen.dart';
import 'wechat/wechat_screen.dart';

class MainScreen extends StatefulWidget {

  const MainScreen({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _MainScreenState();

}

class _MainScreenState extends State<MainScreen> {

  final PageController _pageController = PageController();
  final List<Widget> pages = [
    const HomeScreen(),
    const ImagePickerScreen(),
    const WeChatScreen(),
    const PaperScreen(),
    const CustomViewScreen()
  ];
  var _selectedIndex = 0;
  DateTime? _lastClickBackTime;
  final List<Map<String, String>> bottomItemsData = [
    {"title": "首页", "icon": "ic_home"},
    {"title": "广场", "icon": "ic_square_line"},
    {"title": "公众号", "icon": "ic_wechat"},
    {"title": "体系", "icon": "ic_system"},
    {"title": "项目", "icon": "ic_project"},
  ];

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          body: PageView.builder(
            itemBuilder: (context, index) => pages[index],
            itemCount: pages.length,
            controller: _pageController,
            physics: const NeverScrollableScrollPhysics(),
            onPageChanged: (index) {
              setState(() {
                _selectedIndex = index;
              });
            },
          ),
          bottomNavigationBar: BottomNavigationBar(
            items: _bottomItems(),
            type: BottomNavigationBarType.fixed,
            currentIndex: _selectedIndex,
            onTap: (index) {
              _pageController.jumpToPage(index);
            },
          ),
        ),
        onWillPop: () async {
          if (_lastClickBackTime == null ||
              DateTime.now().difference(_lastClickBackTime!) >
                  const Duration(seconds: 1)) {
            _lastClickBackTime = DateTime.now();
            ToastUtil.show(msg: "再按一次退出程序");
            return false;
          } else {
            return true;
          }
        });
  }

  List<BottomNavigationBarItem> _bottomItems() {
    return bottomItemsData
        .map((map) => buildBottomItem(map['title']!, map['icon']!))
        .toList();
  }

  BottomNavigationBarItem buildBottomItem(String title, String iconName) {
    return BottomNavigationBarItem(
        icon: Image.asset(
          "assets/images/$iconName.png",
          width: 22,
          height: 22,
        ),
        activeIcon: Image.asset(
          "assets/images/$iconName.png",
          width: 22,
          height: 22,
          color: Theme.of(context).primaryColor,
          colorBlendMode: BlendMode.srcIn,
        ),
        label: title);
  }

}
